<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-23 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		require_once ROOT_PATH.'include/logic/image.logic.php';
		
		$this->excute();
	}
	
	function excute()
	{
		switch($this->act)
		{
			case 'list':
				$this->getReviewList();
				break;
			case 'detail';
				$this->getDetail();
				break;
			case 'do_recommend':
				$this->doRecommend();
				break;
			case 'reply_list': // TODO, remove
				$this->getReviewReplyList();
				break;
			case 'add_reply': // TODO, remove
				$this->addReply();
				break;
			case 'vote': // TODO, remove
				$this->addVote();
				break;
			default:
				$this->message('004', STATUS_E);
		}
		
		$this->showResponse();
	}
	
	/**
	 * 
	 * Get movie review list
	 */
	private function getReviewList()
	{		
		if(!isset($this->request['mid']) || !$this->request['mid'])
		{
			$this->message('005', STATUS_E);
		}
		
		$request['from_id'] = $this->from_id;
		$request['to_id'] = $this->to_id;
		$request['page_limit'] = $this->page_limit;
		$request['page_num'] = $this->page_num;
		$request['movie_id'] = (int)$this->request['mid'];
		
		$results = get_movie_review_list($request);
		
		$review_list = array();
		
		if(check_array($results))
		{
			require ROOT_PATH.'servlet/include/output/movie_review.php';
			foreach($results as $item)
			{
				$review_list[] = new movie_review($item);
			}
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($review_list);
		$this->response['list'] = $review_list;
	}
	
	/**
	 * 
	 * Get movie review description
	 */
	private function getDetail()
	{		
		if(!isset($this->request['rid']) || !$this->request['rid'])
		{
			$this->message('005', STATUS_E);
		}
		
		$rid = (int)$this->request['rid'];
		
		$results = get_movie_review_detail($rid);
				
		if(check_array($results))
		{
			if(stripos($results['content'], "[img]") !== false)
			{
				$images = get_movie_images($results['movie_id'], BASE_URL, '2');
				if(check_array($images))
				{
					shuffle($images);
										
					$w = 280;
					$h = 275;
					$image_tags = array();
					foreach($images as $image)
					{
						if(isset($image['image_2']) && $image['image_2'])
						{
							list($w, $h) = getimagesize(str_replace(BASE_URL, ROOT_PATH, $image['image_2']));
							$image_tags[] = "\r\n<img src =\"".$image['image_2']."\" class=\"h288\" width=\"$w\" height=\"$h\"/>\r\n";
						}
					}
					
					$find = array_pad(array(), count($image_tags), "/\[img\]/");
					
					$results['content'] = preg_replace($find, $image_tags, $results['content'], 1);
				}
				
				$results['content'] = str_replace('[img]', '', $results['content']);
			}
			
			require ROOT_PATH.'servlet/include/output/movie_review.php';
			
			$this->response['status'] = STATUS_S;
			$this->response['detail'] = new movie_review($results);
		}
		else
		{
			$this->message('006', STATUS_F);
		}
	}
	
	private function doRecommend()
	{
		// TODO, user should only vote once ?
		
		if(!isset($this->request['rid']) || !$this->request['rid'])
		{
			$this->message('005', STATUS_E);
		}
		
		$request['review_id'] = (int)$this->request['rid'];
		
		$results = get_movie_review_detail($request['review_id']);
		
		if(!check_array($results))
		{
			$this->message('005', STATUS_E);
		}
		
		$results = add_movie_review_recommend_count($request);
				
		if($results)
		{
			$this->message('008', STATUS_S);
		}
		else
		{
			$this->message('009', STATUS_F);
		}
	}
	
	/**
	 * 
	 * Add a reply to movie review
	 */
	function addReply()
	{
		$this->_checkUser();
		
		if(!isset($this->request['rid']) || !$this->request['rid'])
		{
			$this->message('005', STATUS_E);
		}
				
		$request['review_id'] = (int)$this->request['rid'];
		$request['user_id'] = $this->user['id'];
		$request['user_addr'] = $this->request['addr'];
		$request['content'] = $this->request['content'];
		
		$results = get_movie_review_detail($request['review_id']);
		
		if(!check_array($results))
		{
			$this->message('005', STATUS_E);
		}
		if(!$request['content'])
		{
			$this->message('007', STATUS_E);
		}
		
		$results = add_movie_review_reply($request);
				
		if($results)
		{
			$this->message('008', STATUS_S);
		}
		else
		{
			$this->message('009', STATUS_F);
		}
	}
	
	/**
	 * 
	 * Add a vote to movie reply
	 */
	function addVote()
	{
		// TODO, user should only vote once ?
		$this->_checkUser();
		
		if(!isset($this->request['rid']) || !$this->request['rid'])
		{
			$this->message('005', STATUS_E);
		}
				
		$request['review_id'] = (int)$this->request['rid'];
		$request['vote'] = $this->request['vote'];
		
		$results = get_movie_review_detail($request['review_id']);
		
		if(!check_array($results))
		{
			$this->message('005', STATUS_E);
		}
		if(!$request['vote'] || !in_array($request['vote'], array('up', 'down')))
		{
			$this->message('010', STATUS_E);
		}
		
		$results = add_movie_review_vote($request);
				
		if($results)
		{
			$this->message('008', STATUS_S);
		}
		else
		{
			$this->message('009', STATUS_F);
		}
	}
	
	/**
	 * 
	 * Get reply list of movie review
	 */
	function getReviewReplyList()
	{
		$this->_checkUser();
		
		if(!isset($this->request['rid']) || !$this->request['rid'])
		{
			$this->message('005', STATUS_E);
		}
		
		$request['from_id'] = $this->from_id;
		$request['to_id'] = $this->to_id;
		$request['page_limit'] = $this->page_limit;
		$request['page_num'] = $this->page_num;
		$request['review_id'] = (int)$this->request['rid'];
		
		$results = get_movie_review_reply_list($request);
		
		$reply_list = array();
		
		if(check_array($results))
		{
			require ROOT_PATH.'servlet/include/output/movie_review_reply.php';
			foreach($results as $item)
			{
				$reply_list[] = new movie_review_reply($item);
			}
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($reply_list);
		$this->response['list'] = $reply_list;
	}
}